package db4oClass.appClass;

import general.appClass.SystemInfo;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.db4o.ObjectContainer;

import db4oClass.daoClass.Db4oDaoGet;



/**
 * Servlet implementation class db4oImgShowApp
 */
@WebServlet("/db4oImgShowApp")
public class db4oImgShowApp extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public db4oImgShowApp() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("image/jpeg");
		response.setHeader("Pragma","No-Cache");
		response.setHeader("Cache-Control","No-Cache");
		response.setDateHeader("Expires", 0); 
		
		
		//System Information Manage Class
		ObjectContainer container = SystemInfo.getDb4oConn(request);
		int nr = Integer.parseInt(request.getParameter("bnr"));
		BufferedImage 	img 	= null;
		InputStream		in		= null;
		byte[]			data 	= null;
		
		
		if(request.getParameter("type").equals("full_image")) {
			data = Db4oDaoGet.getPhotoDataById(container, nr).getData();
			in = new ByteArrayInputStream(data);
			img = ImageIO.read(in);
		}else if(request.getParameter("type").equals("middle_image")) {
			data = Db4oDaoGet.getPhotoDataById(container, nr).getData();
			in = new ByteArrayInputStream(data);
			BufferedImage full = ImageIO.read(in);
			
			
			int scaledWidth = 0;
			int scaledHeight = 0;
			float divRate = 600 / 450;
			float imgRate = (float) full.getWidth() / (float) full.getHeight();

			if (imgRate > divRate) {
				scaledWidth = 600;
				scaledHeight = (int) (600 / imgRate);
			} else {
				scaledHeight = 450;
				scaledWidth = (int) (450 * imgRate);
			}
			
			Image tmp = full.getScaledInstance(scaledWidth, scaledHeight,
					Image.SCALE_SMOOTH);
			
			img = new BufferedImage(scaledWidth, scaledHeight,
					BufferedImage.TYPE_INT_RGB);
			Graphics g = img.getGraphics();
			g.drawImage(tmp, 0, 0, null);	
		}else if(request.getParameter("type").equals("thumb")) { 
			data = Db4oDaoGet.getThumbByPhotoId(container, nr).getData();
			in = new ByteArrayInputStream(data);
			img = ImageIO.read(in);
		}
		
		OutputStream os = response.getOutputStream();
		ImageIO.write(img, "jpg", os);
		
		in.close();
		os.flush();
		os.close();
	}

}
